import 'package:find_friend/common/alert_tool.dart';
import 'package:find_friend/common/common_widget.dart';
import 'package:find_friend/const/const.dart';
import 'package:find_friend/network/network.dart';
import 'package:flutter/material.dart';

class MsgReportPage extends StatelessWidget {
  
  final String mobile;
  String reportContent = "垃圾广告";
  final FocusNode _focusNode = FocusNode();

  MsgReportPage({Key key, this.mobile}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    
    return Scaffold(
      backgroundColor: APP_scaffold_back_color,
      appBar: AppBar(
        title: Text("举报"),
        backgroundColor: Colors.white,
        centerTitle: true,
        leading: IconButton(
          onPressed: (){
            Navigator.of(context).pop();
          },
          icon: Image.asset("assets/fanhui.png"),
        ),
        actions: [
          FlatButton(
            onPressed: _postAction,
            highlightColor: Colors.transparent,
            splashColor: Colors.transparent,
            child: Container(
              padding: const EdgeInsets.symmetric(horizontal: 11, vertical: 3),
              decoration: BoxDecoration(
                color: APP_main_color,
                borderRadius: BorderRadius.circular(20)
              ),
              child: Text("提交", style: instanceTextStyle(14, Colors.white),)
            ),
          )
        ],
      ),
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: (){
          _focusNode.unfocus();
        },
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Padding(
                padding: const EdgeInsets.all(10.0),
                child: Text("请选择举报理由", style: instanceTextStyle(11, APP_font_grey),),
              ),
              Container(
                margin: const EdgeInsets.symmetric(horizontal: 10),
                padding: const EdgeInsets.only(left: 10, right: 10, bottom: 13),
                decoration: BoxDecoration(
                  color: Colors.white,
                  border: Border.all(color: APP_divider_color, width: 0.5),
                  borderRadius: BorderRadius.circular(10)
                ),
                child: _ReportWidget(focusNode: _focusNode,onContent: (content) => reportContent = content,)
              ),
            ],
          ),
        ),
      ),
    );
  }

  void _postAction() {
    if(reportContent == null) return;
    Networktool.request(API.friendcomplain,params: {"mobile":mobile, "content":reportContent}, success: (data){
      AlertTool.showToast("举报成功");
    });
  }
}

class _ReportWidget extends StatefulWidget {

  final FocusNode focusNode;
  final Function(String content) onContent;

  const _ReportWidget({Key key, this.focusNode, this.onContent}) : super(key: key);

  @override
  __ReportWidgetState createState() => __ReportWidgetState();
}

class __ReportWidgetState extends State<_ReportWidget> {

  List<String> list = [
    "垃圾广告",
    "色情低俗",
    "攻击谩骂",
    "政治敏感",
    "血腥暴力",
    "其他"
  ];

  int selectIdx = 0;

  List<Widget> _buildWidget() {
    List<Widget> temp = [];
    list.forEach((e){
        temp.add(GestureDetector(
          onTap: (){
            setState(() {
              selectIdx = list.indexOf(e);
            });
            if(e != '其他') {
              if(widget.onContent != null) widget.onContent(e);
            }
          },
          child: Container(
            padding: const EdgeInsets.only(top: 15),
            child: Row(
              children: [
                Expanded(child: Text(e, style: instanceTextStyle(14, APP_font_black),)),
                SelectIconWidget(isselect: selectIdx == list.indexOf(e),)
              ],
            ),
          ),
        ));
      });
    if(selectIdx == 5) {
      temp.add(
        Container(
          height: 120.h,
          margin: const EdgeInsets.only(top: 10),
          decoration: BoxDecoration(
            color: APP_scaffold_back_color,
            border: Border.all(color: APP_divider_color, width: 0.5),
            borderRadius: BorderRadius.circular(10)
          ),
          child: TextField(
            maxLines: 10,
            focusNode: widget.focusNode,
            onSubmitted: widget.onContent,
            decoration: InputDecoration(
              border: InputBorder.none,
              hintText: "输入举报理由",
              hintStyle: instanceTextStyle(14, APP_font_deep_grey),
              labelStyle: instanceTextStyle(14, APP_font_black),
              contentPadding: const EdgeInsets.all(10)
            ),
          )
        )
      );
    }
    return temp;
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: _buildWidget(),
    );
  }
}